import {setGlobal,fillZeroWith} from '@swda/utils'

//=============================================================================
// Window_ItemViewer
// 大图预览窗口
//=============================================================================
class Window_ItemViewer extends Window_Base {
    constructor(...list) {
        super(...list)

        this._itemPreview = new Sprite()

        const [x, y, opacity] = [24, 24, 0]
        this._itemPreview.x = x
        this._itemPreview.y = y
        this._itemPreview.opacity = opacity
        this._requestEnemyPreviewRrefresh = false;
        this._item = null;

        this.addChild(this._itemPreview);
    }
    /*
    get viewMap() {
        return new Map([
            ['category', 'items']
        ])
    }
    */
    showView(item) {
        this.show()
        let folderName = "";
        let id = item.id;
        this._item = item;
        let lastBitmap = this._itemPreview.bitmap;
        let newBitmap = null;
        // 护驾
        if(!!item.enemyId){
            id = item.enemyId;
            switch(id){
                case 258:
                    newBitmap = ImageManager.loadSvEnemy('245', $dataEnemies[id].battlerHue);
                    break;
                case 259:
                    newBitmap = ImageManager.loadSvEnemy('245_1', $dataEnemies[id].battlerHue);
                    break;
                case 260:
                    newBitmap = ImageManager.loadSvEnemy('245_2', $dataEnemies[id].battlerHue);
                    break;
                case 261:
                    newBitmap = ImageManager.loadSvEnemy('245_3', $dataEnemies[id].battlerHue);
                    break;
                case 272:
                    newBitmap = ImageManager.loadSvEnemy('252', $dataEnemies[id].battlerHue);
                    break;
                case 273:
                    newBitmap = ImageManager.loadSvEnemy('253', $dataEnemies[id].battlerHue);
                    break;
                default:
                    newBitmap = ImageManager.loadSvEnemy($dataEnemies[id].battlerName,$dataEnemies[id].battlerHue);
                    break;
            }
            if (newBitmap !== lastBitmap){
                this._itemPreview.bitmap = newBitmap;
                this._itemPreview.opacity = 0;
            }
            this._requestEnemyPreviewRrefresh = true;
            return;
        }
        // 其它情况

        
        if (DataManager.isArmor(item))
            // ！！！发布后更新：若旧版本存档背包中存在魔机关卵，则强行将其修复（显示195号敌人数据。
            if(item.baseItemId == 375 && !item.enemyId){
                newBitmap = ImageManager.loadSvEnemy('195', $dataEnemies[195].battlerHue);
                if (newBitmap !== lastBitmap){
                    this._itemPreview.bitmap = newBitmap;
                    this._itemPreview.opacity = 0;
                }
                this._requestEnemyPreviewRrefresh = true;
                return;
            }
            folderName = "armors";
        if (DataManager.isWeapon(item))
            folderName = "weapons";
        if (DataManager.isItem(item))
            folderName = "items";
        if(CP.UseEquipManager.isUseableEquip(item))
            folderName = "items/usableEquip";
        /*
        let folderName = resourceType.toLowerCase()
        // 转换名称
        if (this.viewMap.has(folderName)) {
            folderName = this.viewMap.get(folderName)
        }
        */
        
        if (DataManager.isIndependent(item)){
            id = item.baseItemId;
        }
        id = fillZeroWith(id) // e.g 1-->001,15-->015
        this._itemPreview.bitmap = ImageManager.SWDA_loadPreview(folderName, id);
        this._itemPreview.opacity = this.opacity;
        this._itemPreview.scale.x = 1;
        this._itemPreview.scale.y = 1;
        this._itemPreview.x = 24;
        this._itemPreview.y = 24;
        this._itemPreview.setFrame(0,0,192,288);

    }

    update(){
        Window_Base.prototype.update.call(this);
        this.updateEnemyPreview();
    }

    updateEnemyPreview(){
        if (this._requestEnemyPreviewRrefresh){
            if (this._itemPreview.bitmap.isReady()){
                // 原图尺寸[sw, sh]
                let sw = this._itemPreview.bitmap.width;
                let sh = this._itemPreview.bitmap.height;
                let scale = 0.8;
                // 目标尺寸：[192, 288]
                const [dw,dh] = [192, 288];
                let px = 0;
                let py = 0;
                let pw = sw;
                // 0.8 * 图片高度> 目标高度：将图片按高度缩放为原图的0.8
                if (sh > dh){
                    scale = scale * dh/sh;
                }
                // 特殊情况尺寸修正
                if(!!this._item.enemyId){
                    if([18,252,253,274,275,284].contains(this._item.enemyId)){
                        scale *= 1.22;
                    }
                    if(this._item.enemyId == 285){
                        scale *= 1.8;
                    }
                    if([28,71,93,267].contains(this._item.enemyId)){
                        scale *= 0.75;
                    }
                    if([276, 277, 278].contains(this._item.enemyId)){
                        scale *= 0.6;
                    }
                }
                // 如果上一步缩放后图片宽度 > 目标宽度：截取特定宽度，让其缩放后刚好为目标宽度。
                if ( sw*scale > dw){
                    pw = dw/scale;
                }
                // 截取部分：[px,py,pw,ph]，其中dy是0
                px = (sw-pw)/2;
                this._itemPreview.scale.x = scale;
                this._itemPreview.scale.y = scale;
                this._itemPreview.setFrame(px,py,pw,sh);
                this._itemPreview.opacity = this.opacity;
                // 如果缩放后宽度<目标宽度，或缩放后高度<小于目标高度，则按计算。
                if (sw*scale<dw)
                    this._itemPreview.x = 24 + (dw-sw*scale)/2;
                else
                    this._itemPreview.x = 24;

                if(sh*scale<dh)
                    this._itemPreview.y = 24 + (dh-sh*scale)/2;
                else
                    this._itemPreview.y = 24;


                //特殊情况
                if(!!this._item.enemyId){
                    if([18,252,253,274,275,284].contains(this._item.enemyId)){
                        this._itemPreview.y = 24;
                    }
                }

                this._requestEnemyPreviewRrefresh = false;

            }
        }
    }
}


/*

*/

setGlobal('Window_ItemViewer',Window_ItemViewer)